#ifndef COMMON_H
#define COMMON_H

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <regex.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>



#include "network.h"
#include "relsend.h"
#include "relrecv.h"
#include "timeout.h"

#define BUFSIZE 1200
#define error(s) { perror(s); exit(1); }
	
//Allow use of true and false
enum BOOLEAN {false, true};
	
//Debugging boolean
extern int debug;

/** Handling Messages Function Section **/

// NOTE: The functions are empty represent forward declaration. They are defined common.c, the commented
// functions are their full definitions.

int handleSynMessage(unsigned char * message, char * fileName, short seqNum, char * address, unsigned short port, unsigned long * fileSize);
//void handleSynMessage();
//void handleSynMessage(unsigned char * message, struct addressPort senderInfo);

int handleAckMessage();
//int handleAckMessage(unsigned char * sentMsg, unsigned char * recvMsg, struct addressPort senderInfo, struct messageInfo msgInfo);

void handleFinMessage();
//void handleFinMessage(unsigned char * message, struct addressPort senderInfo);

/** Creating Message Function Section **/

// Struct to hold the message size and payload both by bytes.
struct messageInfo {
  unsigned short messageLength;
  unsigned short messagePayloadSize;
};

// Creates a data message to be sent to the receiver. Used by the relsend application.
void createDataMessage(unsigned char * dataMessage, char * filename, unsigned short seqn, unsigned short messageLength, long int filePosition, struct messageInfo * msgInfo);

// Create a SYN message to be sent to the receiver. Used by the relsend application.
void createSynMessage(unsigned char * synMessage, char * filename, unsigned short seqn, long int fileSize, struct messageInfo * msgInfo);

// Create an ACK message to be sent from the receiver to the sender. Used by the relrecv application.
int createAckMessage(unsigned char * ackMessage, char * filename,  unsigned short ackn);

// Creates a FIN message to be sent to the receiver. Used by the relsend application.
int createFinMessage(unsigned char * finMessage, char * filename);

/** **/

int sizeToBytes(long fileSize, char * buffer, int pos);

int formatNumberByte(int size, int hex);

long power(int num, int power);

#endif